﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GtsServer.Common;
using System.Data.SqlClient;
using System.Data;

namespace GtsServer.DAL
{
    /// <summary>
    /// 数据访问类:DayReport
    /// </summary>
    public partial class DayReportDAL 
    {
        public DayReportDAL()
        { }
        #region  Method

        public DataSet SelectReport(int day)
        {
            IDataParameter[] parms = new IDataParameter[]{ 
                new SqlParameter("@day",day)
            };
            DataSet ds = DbHelperSQL.RunProcedure("SelectReport", parms, "SelectReport", ConnectionEnum.ConnectionReport);
            return ds;
        }

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("ID", "DayReport", ConnectionEnum.ConnectionReport);
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from DayReport");
            strSql.Append(" where ID=@ID");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)
			};
            parameters[0].Value = ID;

            return DbHelperSQL.Exists(strSql.ToString(), parameters, ConnectionEnum.ConnectionReport);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(GtsServer.Model.DayReport model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into DayReport(");
            strSql.Append("TotalUserCount,TotalCharge,TotalPenect,NewUserCount,ActiveUserCount,CoreUserCount,MorrowUserPenect,Price,ChargePerCount,ChargePenect,DayArpu,PayPenect,ReportTime)");
            strSql.Append(" values (");
            strSql.Append("@TotalUserCount,@TotalCharge,@TotalPenect,@NewUserCount,@ActiveUserCount,@CoreUserCount,@MorrowUserPenect,@Price,@ChargePerCount,@ChargePenect,@DayArpu,@PayPenect,@ReportTime)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@TotalUserCount", SqlDbType.Int,4),
					new SqlParameter("@TotalCharge", SqlDbType.Int,4),
					new SqlParameter("@TotalPenect", SqlDbType.Float,8),
					new SqlParameter("@NewUserCount", SqlDbType.Int,4),
					new SqlParameter("@ActiveUserCount", SqlDbType.Int,4),
					new SqlParameter("@CoreUserCount", SqlDbType.Int,4),
					new SqlParameter("@MorrowUserPenect", SqlDbType.Float,8),
					new SqlParameter("@Price", SqlDbType.Int,4),
					new SqlParameter("@ChargePerCount", SqlDbType.Int,4),
					new SqlParameter("@ChargePenect", SqlDbType.Float,8),
					new SqlParameter("@DayArpu", SqlDbType.Float,8),
					new SqlParameter("@PayPenect", SqlDbType.Float,8),
					new SqlParameter("@ReportTime", SqlDbType.DateTime)};
            parameters[0].Value = model.TotalUserCount;
            parameters[1].Value = model.TotalCharge;
            parameters[2].Value = model.TotalPenect;
            parameters[3].Value = model.NewUserCount;
            parameters[4].Value = model.ActiveUserCount;
            parameters[5].Value = model.CoreUserCount;
            parameters[6].Value = model.MorrowUserPenect;
            parameters[7].Value = model.Price;
            parameters[8].Value = model.ChargePerCount;
            parameters[9].Value = model.ChargePenect;
            parameters[10].Value = model.DayArpu;
            parameters[11].Value = model.PayPenect;
            parameters[12].Value = model.ReportTime;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters, ConnectionEnum.ConnectionReport);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(GtsServer.Model.DayReport model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update DayReport set ");
            strSql.Append("TotalUserCount=@TotalUserCount,");
            strSql.Append("TotalCharge=@TotalCharge,");
            strSql.Append("TotalPenect=@TotalPenect,");
            strSql.Append("NewUserCount=@NewUserCount,");
            strSql.Append("ActiveUserCount=@ActiveUserCount,");
            strSql.Append("CoreUserCount=@CoreUserCount,");
            strSql.Append("MorrowUserPenect=@MorrowUserPenect,");
            strSql.Append("Price=@Price,");
            strSql.Append("ChargePerCount=@ChargePerCount,");
            strSql.Append("ChargePenect=@ChargePenect,");
            strSql.Append("DayArpu=@DayArpu,");
            strSql.Append("PayPenect=@PayPenect,");
            strSql.Append("ReportTime=@ReportTime");
            strSql.Append(" where ID=@ID");
            SqlParameter[] parameters = {
					new SqlParameter("@TotalUserCount", SqlDbType.Int,4),
					new SqlParameter("@TotalCharge", SqlDbType.Int,4),
					new SqlParameter("@TotalPenect", SqlDbType.Float,8),
					new SqlParameter("@NewUserCount", SqlDbType.Int,4),
					new SqlParameter("@ActiveUserCount", SqlDbType.Int,4),
					new SqlParameter("@CoreUserCount", SqlDbType.Int,4),
					new SqlParameter("@MorrowUserPenect", SqlDbType.Float,8),
					new SqlParameter("@Price", SqlDbType.Int,4),
					new SqlParameter("@ChargePerCount", SqlDbType.Int,4),
					new SqlParameter("@ChargePenect", SqlDbType.Float,8),
					new SqlParameter("@DayArpu", SqlDbType.Float,8),
					new SqlParameter("@PayPenect", SqlDbType.Float,8),
					new SqlParameter("@ReportTime", SqlDbType.DateTime),
					new SqlParameter("@ID", SqlDbType.Int,4)};
            parameters[0].Value = model.TotalUserCount;
            parameters[1].Value = model.TotalCharge;
            parameters[2].Value = model.TotalPenect;
            parameters[3].Value = model.NewUserCount;
            parameters[4].Value = model.ActiveUserCount;
            parameters[5].Value = model.CoreUserCount;
            parameters[6].Value = model.MorrowUserPenect;
            parameters[7].Value = model.Price;
            parameters[8].Value = model.ChargePerCount;
            parameters[9].Value = model.ChargePenect;
            parameters[10].Value = model.DayArpu;
            parameters[11].Value = model.PayPenect;
            parameters[12].Value = model.ReportTime;
            parameters[13].Value = model.ID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters, ConnectionEnum.ConnectionReport);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from DayReport ");
            strSql.Append(" where ID=@ID");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)
			};
            parameters[0].Value = ID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters, ConnectionEnum.ConnectionReport);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string IDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from DayReport ");
            strSql.Append(" where ID in (" + IDlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), ConnectionEnum.ConnectionReport);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public GtsServer.Model.DayReport GetModel(int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 ID,TotalUserCount,TotalCharge,TotalPenect,NewUserCount,ActiveUserCount,CoreUserCount,MorrowUserPenect,Price,ChargePerCount,ChargePenect,DayArpu,PayPenect,ReportTime from DayReport ");
            strSql.Append(" where ID=@ID");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)
			};
            parameters[0].Value = ID;

            GtsServer.Model.DayReport model = new GtsServer.Model.DayReport();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters, ConnectionEnum.ConnectionReport);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ID"] != null && ds.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    model.ID = int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["TotalUserCount"] != null && ds.Tables[0].Rows[0]["TotalUserCount"].ToString() != "")
                {
                    model.TotalUserCount = int.Parse(ds.Tables[0].Rows[0]["TotalUserCount"].ToString());
                }
                if (ds.Tables[0].Rows[0]["TotalCharge"] != null && ds.Tables[0].Rows[0]["TotalCharge"].ToString() != "")
                {
                    model.TotalCharge = int.Parse(ds.Tables[0].Rows[0]["TotalCharge"].ToString());
                }
                if (ds.Tables[0].Rows[0]["TotalPenect"] != null && ds.Tables[0].Rows[0]["TotalPenect"].ToString() != "")
                {
                    model.TotalPenect = double.Parse(ds.Tables[0].Rows[0]["TotalPenect"].ToString());
                }
                if (ds.Tables[0].Rows[0]["NewUserCount"] != null && ds.Tables[0].Rows[0]["NewUserCount"].ToString() != "")
                {
                    model.NewUserCount = int.Parse(ds.Tables[0].Rows[0]["NewUserCount"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ActiveUserCount"] != null && ds.Tables[0].Rows[0]["ActiveUserCount"].ToString() != "")
                {
                    model.ActiveUserCount = int.Parse(ds.Tables[0].Rows[0]["ActiveUserCount"].ToString());
                }
                if (ds.Tables[0].Rows[0]["CoreUserCount"] != null && ds.Tables[0].Rows[0]["CoreUserCount"].ToString() != "")
                {
                    model.CoreUserCount = int.Parse(ds.Tables[0].Rows[0]["CoreUserCount"].ToString());
                }
                if (ds.Tables[0].Rows[0]["MorrowUserPenect"] != null && ds.Tables[0].Rows[0]["MorrowUserPenect"].ToString() != "")
                {
                    model.MorrowUserPenect = double.Parse(ds.Tables[0].Rows[0]["MorrowUserPenect"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Price"] != null && ds.Tables[0].Rows[0]["Price"].ToString() != "")
                {
                    model.Price = int.Parse(ds.Tables[0].Rows[0]["Price"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ChargePerCount"] != null && ds.Tables[0].Rows[0]["ChargePerCount"].ToString() != "")
                {
                    model.ChargePerCount = int.Parse(ds.Tables[0].Rows[0]["ChargePerCount"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ChargePenect"] != null && ds.Tables[0].Rows[0]["ChargePenect"].ToString() != "")
                {
                    model.ChargePenect = double.Parse(ds.Tables[0].Rows[0]["ChargePenect"].ToString());
                }
                if (ds.Tables[0].Rows[0]["DayArpu"] != null && ds.Tables[0].Rows[0]["DayArpu"].ToString() != "")
                {
                    model.DayArpu = double.Parse(ds.Tables[0].Rows[0]["DayArpu"].ToString());
                }
                if (ds.Tables[0].Rows[0]["PayPenect"] != null && ds.Tables[0].Rows[0]["PayPenect"].ToString() != "")
                {
                    model.PayPenect = double.Parse(ds.Tables[0].Rows[0]["PayPenect"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ReportTime"] != null && ds.Tables[0].Rows[0]["ReportTime"].ToString() != "")
                {
                    model.ReportTime = DateTime.Parse(ds.Tables[0].Rows[0]["ReportTime"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,TotalUserCount,TotalCharge,TotalPenect,NewUserCount,ActiveUserCount,CoreUserCount,MorrowUserPenect,Price,ChargePerCount,ChargePenect,DayArpu,PayPenect,ReportTime ");
            strSql.Append(" FROM DayReport ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append("; select count(1) from  DayReport");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }

            return DbHelperSQL.Query(strSql.ToString(), ConnectionEnum.ConnectionReport);
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(int pageIndex,int pageSize,string strWhere)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("select * from (select *, ROW_NUMBER() OVER(Order by a.ReportTime DESC ) AS RowNumber from DayReport as a  ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" ) as b where RowNumber BETWEEN " + ((pageIndex - 1) * pageSize + 1) + " and " + pageIndex * pageSize + " ");
            strSql.Append("; select count(1) from  DayReport");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }

            return DbHelperSQL.Query(strSql.ToString(), ConnectionEnum.ConnectionReport);
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" ID,TotalUserCount,TotalCharge,TotalPenect,NewUserCount,ActiveUserCount,CoreUserCount,MorrowUserPenect,Price,ChargePerCount,ChargePenect,DayArpu,PayPenect,ReportTime ");
            strSql.Append(" FROM DayReport ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString(), ConnectionEnum.ConnectionReport);
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM DayReport ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString(), ConnectionEnum.ConnectionReport);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.ID desc");
            }
            strSql.Append(")AS Row, T.*  from DayReport T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString(), ConnectionEnum.ConnectionReport);
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "DayReport";
            parameters[1].Value = "ID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  Method
    }
}
